package com.alibaba.citrus.cr.platform.enhance.facade.service.scitem;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crplatformenhance.model.dto.ScItemMultipleExcludeCheckConfigDTO;
import com.epoch.app.crplatformenhance.model.dto.ScItemMultipleExcludeCheckConfigLoadListRequest;
import com.epoch.app.crplatformenhance.model.dto.ScItemMultipleExcludeCheckConfigQueryRequest;
import com.epoch.app.crplatformenhance.scitemmultipleexcludecheckconfig.model.ScItemMultipleExcludeCheckConfig;
import com.epoch.app.crplatformenhance.scitemmultipleexcludecheckconfig.service.ScItemMultipleExcludeCheckConfigService;
import com.epoch.app.crplatformenhance.scitemmultipleexcludecheckconfigread.service.ScItemMultipleExcludeCheckConfigReadService;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 货品倍数排除校验配置读服务
 * @author: yuhw
 * @date: 2023年04月14日 20:34
 **/
@Component
@Primary
public class ScItemMultipleExcludeCheckConfigReadServiceImpl implements ScItemMultipleExcludeCheckConfigReadService {

    private static Log log = Log.getLogger(ScItemMultipleExcludeCheckConfigReadServiceImpl.class);
    @Resource
    private ScItemMultipleExcludeCheckConfigService excludeCheckConfigService;

    /**
     * 查询货品倍数排除校验配置列表
     * @param request
     * @return
     */
    @Override
    @FacadeInvoker
    public Result<List<ScItemMultipleExcludeCheckConfigDTO>> query(ScItemMultipleExcludeCheckConfigQueryRequest request) {
        log.info("scItem_multiple_exclude_check_config_query_request:{}", JSON.toJSONString(request));
        //查询排除货品校验配置
        Result<List<ScItemMultipleExcludeCheckConfig>> listResult = excludeCheckConfigService.loadList(buildLoadListRequest(request));
        List<ScItemMultipleExcludeCheckConfigDTO> configDTOS = Lists.newArrayList();
        if (listResult != null && CollectionUtils.isNotEmpty(listResult.getResult())){
            //转换成对外的DTO
           configDTOS =  listResult.getResult().stream()
                    .map(ScItemMultipleExcludeCheckConfigReadServiceImpl::convert).collect(Collectors.toList());
        }
        return Result.listSuccess(configDTOS,listResult == null ? 0 : listResult.getTotal());
    }

    /**
     * 构建排除货品倍数校验配置查询请求
     * @param request
     * @return
     */
    private ScItemMultipleExcludeCheckConfigLoadListRequest buildLoadListRequest(ScItemMultipleExcludeCheckConfigQueryRequest request){
        ScItemMultipleExcludeCheckConfigLoadListRequest loadListRequest = new ScItemMultipleExcludeCheckConfigLoadListRequest();
        //兼容主键查询
        if (StringUtils.isNotBlank(request.getId())){
            loadListRequest.setId(Long.parseLong(request.getId()));
        }
        //组织id + 发货逻辑仓id + 调拨预报类型
        loadListRequest.setOrganizationId(request.getOrganizationId());
        loadListRequest.setShipWarehouseAreaId(request.getShipWarehouseAreaId());
        loadListRequest.setAllocateApplyType(request.getAllocateApplyType());
        if (request.getStart() != null){
            loadListRequest.setStart(request.getStart());
        }
        if (request.getLimit() != null){
            loadListRequest.setLimit(request.getLimit());
        }
        //更新时间倒序
        loadListRequest.setDir("gmt_modified");
        loadListRequest.setAsc(Boolean.FALSE);
        return loadListRequest;
    }

    // 转换模型 属性赋值
    private static ScItemMultipleExcludeCheckConfigDTO convert(ScItemMultipleExcludeCheckConfig config){
        if (config == null){
            return null;
        }
        ScItemMultipleExcludeCheckConfigDTO configDTO = new ScItemMultipleExcludeCheckConfigDTO();
        configDTO.setId(config.getId());
        configDTO.setOrganizationCode(config.getOrganizationCode());
        configDTO.setOrganizationName(config.getOrganizationName());
        configDTO.setOrganizationId(config.getOrganizationId());
        configDTO.setShipWarehouseAreaCode(config.getShipWarehouseAreaCode());
        configDTO.setShipWarehouseAreaName(config.getShipWarehouseAreaName());
        configDTO.setShipWarehouseAreaId(config.getShipWarehouseAreaId());
        configDTO.setAllocateApplyType(config.getAllocateApplyType());
        configDTO.setAllocateApplyTypeDesc(config.getAllocateApplyTypeDesc());
        configDTO.setGmtCreate(config.getGmt_create());
        configDTO.setGmtModified(config.getGmt_modified());
        return configDTO;
    }
}
